import 'dart:async';
import 'dart:ffi';

import 'package:flutter/services.dart';

class UpdateInfo {
  /// 是否有新版本回调
  /// hasUpgrade true: 有 false: 没有
  /// message 升级提示消息
  /// version 升级版本号

  bool? hasUpgrade;
  String? message;
  String? version;

  UpdateInfo(this.hasUpgrade, this.message, this.version);
}

class PluginJdUpgrade {
  static const MethodChannel _channel = MethodChannel('plugin_jd_upgrade');

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  ///检查更新并弹框
  static Future<void> checkAndPop() async {
    await _channel.invokeMethod("checkAndPop");
  }

  ///检查更新并弹框，有时间间隔，具体查看后端配置
  static Future<void> limitedCheckAndPop() async {
    await _channel.invokeMethod("limitedCheckAndPop");
  }

  ///检查更新，如果有更新弹框提示下载，没有提示暂无更新
  static Future<UpdateInfo> checkUpdate() async {
    dynamic res = await _channel.invokeMethod("checkUpdate");
    return UpdateInfo(res["hasUpgrade"], res["message"], res["version"]);
  }
}
